package com.ocean.web.controller.product;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.productDict.domain.OZnwgSt;
import com.ocean.productDict.domain.bZt;
import com.ocean.productDict.domain.oNcfileMonitor;
import com.ocean.productDict.service.oNcfileMonitorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ocean.common.config.datasource.DynamicDataSourceContextHolder.log;

/**
 * 2024\1新功能 nc文件监控
 */
@Controller
@RequestMapping("/product/ncfilemonitor")
public class oNcfileMonitorController {

    @Autowired
    private oNcfileMonitorService oNcfileMonitorService;

    /**
     * 产品介绍L类别查询
     *
     * @return
     */
    @PostMapping("/ltypejb")
    @ResponseBody
    public PageInfo ltypejb(oNcfileMonitor oNcfileMonitor) {
        //List<oNcfileMonitor> list = oNcfileMonitorService.gettype(type);
        // 设置分页
        PageHelper.startPage(oNcfileMonitor.getCurrentPage(), oNcfileMonitor.getPageSize());
        List<oNcfileMonitor> list = oNcfileMonitorService.gettypedata(oNcfileMonitor);
        PageInfo<oNcfileMonitor> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @RequestMapping("/download")
    public void download(String path, HttpServletResponse response) {
        try {
            // path是指想要下载的文件的路径
            File file = new File(path);
            log.info(file.getPath());
            // 获取文件名
            String filename = file.getName();
            // 获取文件后缀名
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toLowerCase();
            log.info("文件后缀名：" + ext);

            // 将文件写入输入流
            FileInputStream fileInputStream = new FileInputStream(file);
            InputStream fis = new BufferedInputStream(fileInputStream);
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();

            // 清空response
            response.reset();
            // 设置response的Header
            response.setCharacterEncoding("UTF-8");
            //Content-Disposition的作用：告知浏览器以何种方式显示响应返回的文件，用浏览器打开还是以附件的形式下载到本地保存
            //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
            // filename表示文件的默认名称，因为网络传输只支持URL编码的相关支付，因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
            // 告知浏览器文件的大小
            response.addHeader("Content-Length", "" + file.length());
            OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            outputStream.write(buffer);
            outputStream.flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 监控智能网格数据
     * @param type
     * @return
     */
    @PostMapping("/getjiankong")
    @ResponseBody
    public AjaxResult getjiankong(oNcfileMonitor oNcfileMonitor) {

        List<oNcfileMonitor> list = oNcfileMonitorService.selectdata(oNcfileMonitor);
        return AjaxResult.success(list);
    }

    /**
     * 最近文件
     * @param oNcfileMonitor
     * @return
     */
    @PostMapping("/getzuijin")
    @ResponseBody
    public AjaxResult getzuijin() {
        List<oNcfileMonitor> list = oNcfileMonitorService.getDtdatasj();
    return AjaxResult.success(list);
    }
    /**
     * 获取所有台区
     * @return
     */
    @PostMapping("/gettq")
    @ResponseBody
    public AjaxResult gettq(OZnwgSt oZnwgSt) {

        List<OZnwgSt> list = oNcfileMonitorService.gettq(oZnwgSt);
        return AjaxResult.success(list);
    }

    /**
     * 文件总数
     * @return
     */
    @PostMapping("/wjzs")
    @ResponseBody
    public AjaxResult wjzs() {
        Map map = new HashMap();
        List<oNcfileMonitor> list = oNcfileMonitorService.getfilesize();//总文件数
        map.put("filenum",list.size());
        List<OZnwgSt> dtlist = oNcfileMonitorService.getfilesizedt();//当天的文件数量
        map.put("filedtnum",dtlist.size());
        List<oNcfileMonitor> listdt = oNcfileMonitorService.getfilesizedtsl();//当天文件数
        double zhdt = 0.00000000000000;
        for (int i = 0; i < listdt.size(); i++) {
            long l = Long.parseLong(listdt.get(i).getFilesize());
            double v = convertBytesToGB(l);
            zhdt = zhdt+v;
        }
        map.put("jgsize",zhdt);//当天文件大小
        List<oNcfileMonitor> listdn = oNcfileMonitorService.getfilesizedn();//当年文件数
        double zhdn = 0.00000000000000;
        for (int i = 0; i < listdn.size(); i++) {
            long l = Long.parseLong(listdn.get(i).getFilesize());
            double v = convertBytesToGB(l);
            zhdn = zhdn+v;
        }
        map.put("cptype",zhdn);//当年文件大小
        double zh = 0.00000000000000;
        for (int i = 0; i < list.size(); i++) {
            long l = Long.parseLong(list.get(i).getFilesize());
            double v = convertBytesToGB(l);
            zh = zh+v;
        }
        map.put("filesize",zh);//文件大小
        return AjaxResult.success(map);
    }

    /**
     * 获取七天的折线图数据
     * @return
     */
    @PostMapping("/get7data")
    @ResponseBody
    public AjaxResult get7data() {
        Map oNcfileMonitorService7data = oNcfileMonitorService.get7data();
        return AjaxResult.success(oNcfileMonitorService7data);
    }
    /**
     * 获取当天饼状图
     * @return
     */
    @PostMapping("/getdtdata")
    @ResponseBody
    public AjaxResult getdtdata() {
        List<bZt> map = oNcfileMonitorService.getdtdata();
        return AjaxResult.success(map);
    }
    /**
     * 将字节数转换为GB。
     *
     * @param bytes 字节数
     * @return 以GB为单位的表示
     */
    public static double convertBytesToGB(long bytes) {
        final long BYTES_IN_GB = 1024L * 1024L * 1024L;
        return (double) bytes / BYTES_IN_GB;
    }

}
